﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using WebApplication3.Models;

namespace WebApplication3.Controllers
{
	 
	public class BmobOrderController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
		[ActionName(name: "pagelist")]
		public async Task<JsonResult> GetpageAsync(   )
		{
			var wc = System.Net.HttpWebRequest.Create("https://api2.bmob.cn/1/classes/orders")
			;
			wc.Method = "GET";
			wc.Headers.Add("X-Bmob-Application-Id", "3b7bc81ed038b60c3cc63922ede49a33");
			wc.Headers.Add("X-Bmob-REST-API-Key", "f76a0a13d6ac18cc84cd255a940011cd");
			wc.ContentType = "application/json";

			var req = Request.Query;
			var name = Request.Query["key[name]"].ToString();
			var mobile = Request.Query["key[mobile]"].ToString();
			var address = Request.Query["key[address]"].ToString();
			var status = Request.Query["key[status]"].ToString();

			var where = "where ={";
			if (!string.IsNullOrWhiteSpace(name))
			{
				where += "  \"ReceiveName\":\"" + name + "\",";

			}
			if (!string.IsNullOrWhiteSpace(mobile))
			{
				where += "  \"ReceiveMobile\":\"" + mobile + "\",";

			}

			if (!string.IsNullOrWhiteSpace(address))
			{
				where += "  \"ReceiveAddress\":\"" + address + "\",";

			}

			 
			if (!string.IsNullOrWhiteSpace(status ))
			{
				where += "  \"Status\": " + status + " ,";
				 
			}
			where += "}";
			//var url = $"https://api2.bmob.cn/1/cloudQuery?bql=select * from orders limit 0,100 order by createdAt desc'";
			var  result= await	HttpHelper.GetUrlAsync("https://api2.bmob.cn/1/classes/orders?"+ where);
			try
			{
				var list = result.DeserializeObject<JObject>();
				return Json(new LayuiPageList<BmobOrderViewModel> {
					code = 0, msg = "", count = 1000, data = list["results"]
				})  ;
			}
			catch (Exception ex)
			{
				return Json(new LayuiPageList<BmobOrderViewModel>
				{
					code = 1,
					msg = "获取失败",
					count = 0,
					data = null
				});


			}
		
			
		}
	}
}